package com.tanhua.server.service;


import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.dubbo.api.PeachBlossomApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.mongo.PeachBlossom;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PeachBlossomVO;
import com.tanhua.server.interceptor.UserHolder;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;
import java.util.Random;

@Service
public class PeachblossomService {

    @DubboReference
    private PeachBlossomApi peachBlossomApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private OssTemplate ossTemplate;
    //发送语音
    public void sendVoice(MultipartFile soundFile) throws IOException {
        //1.把语音存储到阿里云
        String soundPath = ossTemplate.upload(soundFile.getOriginalFilename(), soundFile.getInputStream());
        /*//获取音频文件原始文件名
        String originalFilename = soundFile.getOriginalFilename();
        //获取文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        //上传到fastdfs服务器上需要三个参数:1.文件输入流、2.文件大小、3.文件后缀名
        StorePath uploadFile = fastFileStorageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), suffix, null);
        //获取文件路径
        String fullPath = uploadFile.getFullPath();
        //拼接文件路径
        String soundPath = fdfsWebServer.getWebServerUrl() + fullPath;*/
        System.out.println("文件存储到:" + soundPath);
        //3.把音频文件信息存储到MongoDB中
       // peachBlossom.setId(ObjectId.get());
        peachBlossomApi.saveSound(UserHolder.getUserId(),soundPath);
        //保存当日接收次数
        redisTemplate.opsForValue().set("RemainingTimes_" + UserHolder.getUserId(), "10", Duration.ofDays(1));
    }


    //接收语音
    public ResponseEntity receivingVoice() {
        //1.从MongoDB中随机查询一个音频信息
        //  long userId = new Random().nextInt(10) + 1;
        Long userId = UserHolder.getUserId();
        PeachBlossom peachBlossom = peachBlossomApi.findVoice(userId);
        Long userId1 = peachBlossom.getUserId();
        //2.调用Api查询用户信息
        UserInfo userInfo = userInfoApi.findById(userId1);

        //3.从redis中获取今日剩余次数
        String count = redisTemplate.opsForValue().get("RemainingTimes_" + UserHolder.getUserId());
        //封装返回数据
        PeachBlossomVO vo = new PeachBlossomVO();
        vo.setId(peachBlossom.getUserId());
        vo.setAge(userInfo.getAge());
        vo.setAvatar(userInfo.getAvatar());
        vo.setGender(userInfo.getGender());
        vo.setNickname(userInfo.getNickname());
        vo.setSoundUrl(peachBlossom.getSoundUrl());
        vo.setRemainingTimes(Integer.parseInt(count));
        //将今日查询次数减一
        redisTemplate.opsForValue().decrement("RemainingTimes_" + UserHolder.getUserId());
        //返回数据
        return ResponseEntity.ok(vo);
    }
}
